<template>
  <div>
        <h3 style="color: red">赌博的奥义</h3>
        <el-button @click="gamble">gamble开始</el-button>
  </div>
</template>

<script>
    export default {
        name: "Example1",
      data(){
        return{

          interval:''
        }
      },
      methods:{
        gamble(){
          //promise版本 普通版本在下面注释部分
          let money = 2
          new Promise((resolve,reject)=>{

              this.interval=  setInterval(()=>{
                if (money>=0){
                  let x = Math.floor(Math.random()*2) ;
                  // console.log("x:"+x);
                  if(x===0){
                    money+=1;
                  }else{
                    money-=1;
                  }

                  if (money === 0) {
                    clearInterval(this.interval)
                    console.log("现金:" + money);
                    resolve(money)
                  } else {
                    console.log("现金:" + money);
                  }
                }
                },100)


            }).then((value)=>{
              console.log(`对不起你的金额剩余:${value},游戏结束`);
          })

        }
      }
    }
</script>

<style scoped>

</style>

<!--

  var money = 1;
  function f1(callback) {
    window.interval = setInterval(function () {
      /*    var f = function (cash) {
            if(cash == 0){
              console.log("现金:"+cash,"对不起!game over");
              clearInterval(interval)
            }else {
              console.log("现金:"+cash);
            }

          }*/
      var x = Math.floor(Math.random()*2) ;
      // console.log("x:"+x);
      if(x==0){
        money+=1;
        callback(money)
      }else{
        money-=1;
        callback(money)
      }
    },100)
  }


  f1(function (cash) {
    if (cash == 0) {
      console.log("现金:" + cash, "对不起!game over");
      clearInterval(interval)
    } else {
      console.log("现金:" + cash);
    }
  })




-->
